#ifndef EVENTLOOP_EVENTLOOP_H
#define EVNETLOOP_EVENTLOOP_H


#include "utility/Noncopyable.h"
#include "utility/chrono.h"
#include "thread/current_thread.h"

#include <memory>

#include <vector>

class Poller;
class Channel;

class Eventloop
{
public:
    static Eventloop *LOOP_OF_CURRENT_THREAD();

    Eventloop(/* args */);
    ~Eventloop();

    void loop();
    void quit(); 

    void update_channel(Channel *channel);
    void remove_channel(Channel *channel);

    Utility::timepoint poll_return_time() const { return _pollReturnTime; }

    void assert_in_this_thread(const char *func, const char *file, int line) const;
    bool is_in_this_thread() const;

private:
    NONCOPYABLE(Eventloop);
    using ChannelList = std::vector<Channel *>;

    bool _looping;
    const int _tid;
    bool _quit;
    bool _eventHandling;
    Utility::timepoint _pollReturnTime;
    std::unique_ptr<Poller> _poller;
    ChannelList _activeChannels;
    Channel *_currentActiveChannel;
};


#define ASSERT_IN_THIS_THREAD() \
    assert_in_this_thread(__func__, __FILE__, __LINE__)
#endif